package it.univpm.diiga.memorysim.kernel.shared.tree;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;




public class MovingBoxLabelProvider extends LabelProvider {	
	private Map<Object, Object> imageCache = new HashMap<Object, Object>(11);
	
	/*
	 * @see ILabelProvider#getImage(Object)
	 	class ViewLabelProvider extends LabelProvider {

		public String getText(Object obj) {
			return obj.toString();
		}
		public Image getImage(Object obj) {
			String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
			if (obj instanceof MovingBox)
			   imageKey = ISharedImages.IMG_OBJ_FOLDER;
			return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
		}
	}*/
	
	public Image getImage(Object element) {
		String imageKey = ISharedImages.IMG_OBJ_FOLDER;
		if(((MovingBox) element).getName().equals("Disco")) imageKey = ISharedImages.IMG_ETOOL_HOME_NAV;
		if (element instanceof File)
			imageKey = ISharedImages.IMG_OBJ_FILE;
		return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
	}

	/*
	 * @see ILabelProvider#getText(Object)
	 */
	public String getText(Object element) {
		if (element instanceof MovingBox) {
			if(((MovingBox)element).getName() == null) {
				return "Box";
			} else {
				return ((MovingBox)element).getName();
			}
		} else if (element instanceof File) {
			return ((File)element).getTitle();
		} else {
			throw unknownElement(element);
		}
	}

	public void dispose() {
		for (Iterator<Object> i = imageCache.values().iterator(); i.hasNext();) {
			((Image) i.next()).dispose();
		}
		imageCache.clear();
	}

	protected RuntimeException unknownElement(Object element) {
		return new RuntimeException("Unknown type of element in tree of type " + element.getClass().getName());
	}

}
